package com.ruibang.glass.material.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.GlobalUser;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.constant.MaterialErrorCode;
import com.ruibang.glass.material.domain.req.ContainerManagerReq;
import com.ruibang.glass.material.domain.req.ContainerTransferReq;
import com.ruibang.glass.material.domain.resp.GlassContainerWarehouseInfo;
import com.ruibang.glass.material.entity.ContainerTransferRecord;
import com.ruibang.glass.material.entity.GlassContainerManager;
import com.ruibang.glass.material.mapper.GlassContainerManagerMapper;
import com.ruibang.glass.material.service.ContainerTransferRecordService;
import com.ruibang.glass.material.service.GlassContainerManagerService;
import com.ruibang.glass.material.service.MaterialWarehouseInfoService;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import com.teaming.cloud.framework2.common.model.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 容器管理表 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-03-25
 */
@Service
public class GlassContainerManagerServiceImpl extends ServiceImpl<GlassContainerManagerMapper, GlassContainerManager> implements GlassContainerManagerService {

    @Autowired
    private ContainerTransferRecordService containerTransferRecordService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private MaterialWarehouseInfoService materialWarehouseInfoService;

    @Override
    public IPage<GlassContainerManager> pageQuery(ContainerManagerReq containerManagerReq) {
        return baseMapper.pageQuery(new Page<>(containerManagerReq.getPageNo(), containerManagerReq.getPageSize()), containerManagerReq);
    }


    @Override
    public GlassContainerManager getByContainerCode(String contentCode) {
        return baseMapper.getByContainerCode(contentCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(GlassContainerManager containerManager) {
        //保存容器
        try {
            containerManager.setIsDelete(Constants.ZERO_STR);
            this.save(containerManager);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                throw new ServiceException(MaterialErrorCode.CONTAINER_CODE_REPEAT.getCode(), MaterialErrorCode.CONTAINER_CODE_REPEAT.getMsg());
            }
            throw new ServiceException(MaterialErrorCode.SAVE_CONTAINER_FAIL.getCode(), MaterialErrorCode.SAVE_CONTAINER_FAIL.getMsg());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody transfer(ContainerTransferReq containerTransferReq) {
        //获取容器
        String containerId = containerTransferReq.getContainerId();
        GlassContainerManager glassContainerManager = this.getById(containerId);
        if (glassContainerManager == null) {
            return ResultBody.fail(MaterialErrorCode.CONTAINER_NOT_FOUND.getCode(), MaterialErrorCode.CONTAINER_NOT_FOUND.getMsg());
        }

        //根据库位码获取仓库、库位与状态信息
        String warehousePositionCode = containerTransferReq.getWarehousePositionCode();
        GlassContainerWarehouseInfo glassContainerWarehouseInfo = materialWarehouseInfoService.getWarehouseInfoByWarehousePositionCode(warehousePositionCode);
        if (glassContainerWarehouseInfo == null || StringUtils.isBlank(glassContainerWarehouseInfo.getWarehouseId()) || StringUtils.isBlank(glassContainerWarehouseInfo.getWarehousePositionId())) {
            return ResultBody.fail(MaterialErrorCode.WAREHOUSE_OR_POSITION_IS_NULL.getCode(), MaterialErrorCode.WAREHOUSE_OR_POSITION_IS_NULL.getMsg());
        }

        //修改容器信息
        glassContainerManager.setStatusId(glassContainerWarehouseInfo.getStatusId());
        glassContainerManager.setWarehouseId(glassContainerWarehouseInfo.getWarehouseId());
        glassContainerManager.setWarehousePositionId(glassContainerWarehouseInfo.getWarehousePositionId());
        this.updateById(glassContainerManager);

        //保存容器转移记录
        ContainerTransferRecord containerTransferRecord = new ContainerTransferRecord();
        containerTransferRecord.setTransferTime(LocalDateTime.now());
        containerTransferRecord.setContainerCode(glassContainerManager.getContainerCode());
        containerTransferRecord.setContainerType(glassContainerManager.getContainerType());

        containerTransferRecord.setStatusId(glassContainerWarehouseInfo.getStatusId());
        containerTransferRecord.setWarehouseId(glassContainerWarehouseInfo.getWarehouseId());
        containerTransferRecord.setWarehousePositionId(glassContainerWarehouseInfo.getWarehousePositionId());

        containerTransferRecord.setStatusName(glassContainerWarehouseInfo.getStatusName());
        containerTransferRecord.setWarehouseName(glassContainerWarehouseInfo.getWarehouseName());
        containerTransferRecord.setWarehousePositionName(glassContainerWarehouseInfo.getWarehousePositionName());


        //获取用户信息
        GlobalUser user = commonService.getUser();
        if (user != null) {
            containerTransferRecord.setTransferUserName(user.getName());
            containerTransferRecord.setTransferUserId(user.getUserId());
        }
        containerTransferRecordService.save(containerTransferRecord);

        return ResultBody.success();
    }

    @Override
    public void edit(GlassContainerManager containerManager) {
        //修改容器
        try {
            //更新人
            GlobalUser user = commonService.getUser();
            if (user != null && StringUtils.isNotBlank(user.getUserId())) {
                containerManager.setUpdateUserId(user.getUserId());
                containerManager.setUpdateUserName(user.getName());
            }
            this.updateById(containerManager);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                throw new ServiceException(MaterialErrorCode.CONTAINER_CODE_REPEAT.getCode(), MaterialErrorCode.CONTAINER_CODE_REPEAT.getMsg());
            }
            throw new ServiceException(MaterialErrorCode.SAVE_CONTAINER_FAIL.getCode(), MaterialErrorCode.SAVE_CONTAINER_FAIL.getMsg());
        }
    }


    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2024, 3, 1);
        LocalDate endDate = LocalDate.of(2024, 3, 10);
        List<LocalDate> dateRange = getDateRange(startDate, endDate);
        for (LocalDate date : dateRange) {
            System.out.println(date);
        }
    }


    public static List<LocalDate> getDateRange(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> dateList = new ArrayList<>();
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            dateList.add(currentDate);
            currentDate = currentDate.plusDays(1);
        }
        return dateList;
    }
}
